﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PPAP.BO;
using PPAP.Model.Entity;
using PPAP.Model;

namespace PPAP.Web.Supplier
{
    public partial class SupplierEdit : PageBase
    {
        SupplierBO bo = new SupplierBO();
        UsersBO ubo = new UsersBO();
        ContractPersonBO pbo = new ContractPersonBO();
        SUPPLIER su = new SUPPLIER();
        MailTemplateBO mbo = new MailTemplateBO();
        LogBO lbo = new LogBO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //if (Request.QueryString["RID"] != null)
                //{
                string rid = "";
                if (Request.QueryString["RID"] != null)
                    rid = Request.QueryString["RID"].ToString();
                else if (CurrentUser.UnitId == "0" || CurrentUser.UnitId == "99")
                    rid = CurrentUser.UnitName;
                else
                    rid = "0";
                 su = bo.GetModel(rid);
                    if (su != null)
                    {
                        txtAddress.Text = su.ADDRESS;
                        txtContacttel.Text = su.CONTACTTEL;
                        //txtDSC.Text = su.DSC;
                        //txtPassword.Text = su.PASSWORD;
                        //txtPasswordaffirm.Text = su.PASSWORD;
                        txtSupplierCode.Text = su.SUPPLIERCODE;
                        txtSupplierName.Text = su.SUPPLIERNAME;
                        txtUserName.Text = su.USERNAME;
                        //ddlSupplierLevel.SelectedValue = "";
                        txtCountry.Text = su.COUNTRY;
                        txtProvince.Text = su.PROVINCE;
                        txtPostalCode.Text = su.POSTALCODE;
                        txtCity.Text = su.CITY;
                        
                        if (CurrentUser.UnitId == "99")
                        {
                            txtSupplierName.Enabled = false;
                            txtSupplierCode.Enabled = false;
                        }
                    }
                //}
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            //if (txtPassword.Text != txtPasswordaffirm.Text)
            //{
            //    base.AlertMessage("密码不一致");
            //    return;
            //}

            int num = 0;
            if (txtSupplierName.Text.Trim() == "" || txtSupplierCode.Text.Trim() == "" )
            {
                AlertMessage("The text frame not is empty");
                return;
            }
            //if (Request.QueryString["RID"] != null || Session["SupplierInfo"] != null)
            //{
            string rid = "";
            if (Request.QueryString["RID"] != null)
                rid = Request.QueryString["RID"].ToString();
            else if (CurrentUser.UnitId == "0" || CurrentUser.UnitId == "99")
                rid = CurrentUser.UnitName;
            else
                rid = "0";
                su = bo.GetModel(rid);
            //}
            if (su == null)
                su = new SUPPLIER();
            //su.PASSWORD = txtPassword.Text;
            su.SUBMISSIONLEVEL ="";
            su.SUPPLIERCODE = CheckWord(txtSupplierCode.Text);
            su.SUPPLIERNAME = CheckWord(txtSupplierName.Text);
            su.USERNAME =CheckWord(txtUserName.Text.ToLower());
            su.DSC = "";
            su.CONTACTTEL = CheckWord(txtContacttel.Text);
            su.ADDRESS = CheckWord(txtAddress.Text);
            su.CITY = CheckWord(txtCity.Text);
            su.COUNTRY = CheckWord(txtCountry.Text);
            su.POSTALCODE = CheckWord(txtPostalCode.Text);
            su.PROVINCE = CheckWord(txtProvince.Text);
            if (Request.QueryString["RID"] == null&&CurrentUser.UnitId!="0"&&CurrentUser.UnitId!="99")
            {
                //if (bo.GetList(" and username='"+su.USERNAME+"'").Tables[0].Rows.Count > 0)
                //{
                //    AlertMessage("User name already exists");
                //    return;
                //}
                //if (ubo.CheckEamil(su.USERNAME))
                //{
                //    AlertMessage("User name already exists");
                //    return;
                //}
                if (bo.GetList(" and SUPPLIERCODE='" + su.SUPPLIERCODE + "'").Tables[0].Rows.Count > 0)
                {
                    AlertMessage("SUPPLIERCODE already exists");
                    return;
                }

                su.PASSWORD = "123456";
                num = bo.Add(su);
                //ContractPerson cb = new ContractPerson();
                ////cb.SUPPLIERID = num;
                //cb.EMAIL = su.USERNAME;
                //cb.Password = su.PASSWORD;
                //cb.NAME = su.SUPPLIERNAME;
                //cb.POSTITION = "Admin";
                //cb.GENDER = "male";
                //cb.STATUS = "active";
                //cb.TELEPHONE = su.CONTACTTEL;
                //pbo.Add(cb);

                //发邮件
                //MailTemplate mail = null;
                //    mail = mbo.GetModel(15);
                //if (mail != null)
                //{
                //    string title = mail.TITLE.Replace("{supplier name}", su.SUPPLIERNAME);
                //    try
                //    {

                //        base.SendEmailToUser(su.USERNAME, title, mail.CONTENT.Replace("{supplier name}", su.SUPPLIERNAME));
                //        lbo.WriteLog(CurrentUser.Id, "Send Email:" + su.USERNAME);

                //    }
                //    catch (Exception ex)
                //    {
                //        AlertMessage(ex.Message.ToString());
                //        lbo.WriteLog(CurrentUser.Id, "Send Email:" + ex.Message.ToString());
                //    }
                //}

                lbo.WriteLog(CurrentUser.Id, "Add supplier:"+su.SUPPLIERNAME);
                //Session["SupplierInfo"] = num;
            }
            else
            {
                num = bo.Update(su);
                lbo.WriteLog(CurrentUser.Id, "Edit supplier:"+su.SUPPLIERNAME);
            }

            //if (num > 0 && CurrentUser.UnitId == "0")
            //    base.AlertMessage("Save success");
            //else
            if (num > 0 && CurrentUser.UnitId != "0")
                base.AlertReflashOpener("Save success");
        }
    }
}